package com.xykj.enterprise.wechat.ext.api.externalcontact;

import com.xykj.enterprise.wechat.bean.ext.externalcontact.TransferCustomerVo;
import com.xykj.enterprise.wechat.ext.service.customer.ExternalContactService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Function(description = "分配在职成员的客户", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "handover_userid", type = Type.String, description = "原跟进成员的userid", required = true),
        @Parameter(name = "takeover_userid", type = Type.String, description = "接替成员的userid", required = true),
        @Parameter(name = "external_userid", type = Type.String, description = "客户的external_userid列表，每次最多分配100个客户", required = true),
        @Parameter(name = "transfer_success_msg", type = Type.String, description = "转移成功后发给客户的消息，最多200个字符，不填则使用默认文案", required = false),
})
@Component
public class TransferCustomer implements Action {

    @Autowired
    ExternalContactService externalContactService;

    @Override
    public Response execute(Request request) throws Exception {
        String handover_userid = request.getString("handover_userid");
        String takeover_userid = request.getString("takeover_userid");
        String external_userid = request.getString("external_userid");
        String transfer_success_msg = request.getString("transfer_success_msg");
        String[] externalUseridSplit = external_userid.split(",");
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("handover_userid", handover_userid);
        params.put("takeover_userid", takeover_userid);
        params.put("external_userid", externalUseridSplit);
        if (transfer_success_msg != null) {
            params.put("transfer_success_msg", transfer_success_msg);
        }

        TransferCustomerVo rsp = externalContactService.transferCustomer(
                request.getString("access_token"), params
        );
        if (rsp.getErrcode() != 0) {
            return Response.fail(rsp.getErrmsg());
        }
        return Response.success().put("data", rsp);
    }
}
